﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Project;
using Jzy.Model.Project;
using Jzy.Core.Common.Web;
using System.Collections.Generic;
using Jzy.Model.Calendar;
using Jzy.BLL.Calendar;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Core.Dict;


public partial class Project_Task_Edit : PageEdit
{
    private PrjTaskBO _task ;
    private string PrjID
    {
        get
        {
            return Request["PrjID"];
        }
    }

   
    protected void Page_Load(object sender, EventArgs e)
    {
        _task = new PrjTaskBO(int.Parse(PrjID));

        if (!IsPostBack)
        {
            BindCalendarType();
            if (PKID != null)
            {
                DispInfo();               
            }
            else
            {
                UpperID.Value = StrUtil.DecryptDES(Request["UpperID"]);
                StartTime.Text = EndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                TimeLimit.Text = "1";
                IsPrediction.Checked = true;
                //chkCalendar.Checked = true;
            }
        }        
        AddAttr();
    }

    private void BindCalendarType()
    {
        DictBO dict = new DictBO();
        dict.BindListControl(drpCalendarTypeID, (int)DictItem.日程类型, "Code");  
    }

    private void AddAttr()
    {
        //JSUtil.ShowCalendar(StartTime, false);
        //JSUtil.ShowCalendar(EndTime, false);

        WebUtil.SetTimeBox(StartTime, false);


        WebUtil.SetTimeBox(EndTime, false);

        JSUtil.ShowDlg(UserIDs, UserNames, "SelectUser.aspx?PrjID=" + PrjID);

        StartTime.Attributes.Add("onblur", "setTimeLimit()");
        EndTime.Attributes.Add("onblur", "setTimeLimit()");

        chkCalendar.Attributes.Add("onclick", "chkCalendar_Click(this,'" + lblCalendarTypeID.ClientID + "','" + drpCalendarTypeID.ClientID + "')");

        if (string.IsNullOrEmpty(PKID))
        {
            pnlMain.Style.Add("height", "259px");
        }
        else
        {
            pnlMain.Style.Add("height", "309px");
        }
    }

    private void DispInfo()
    {
        PrjTaskInfo info = _task.GetInfo(PKID);
        
        EntityUtil.SetControl(pnlMain, info);
        if (info.StartTime == DateTime.MinValue)
        {
            StartTime.Text = "";
        }
        else
        {
            StartTime.Text = info.StartTime.ToString("yyyy-MM-dd");
        }
        if (info.EndTime == DateTime.MinValue)
        {
            EndTime.Text = "";
        }
        else
        {
            EndTime.Text = info.EndTime.ToString("yyyy-MM-dd");
        }

        
        trReason.Visible = true;
        

        if (_task.HasChild(int.Parse(PKID)))
        {
            StartTime.Enabled = false;
            EndTime.Enabled = false;
            TimeLimit.Enabled = false;

            StartTime.ToolTip = EndTime.ToolTip = TimeLimit.ToolTip = "由于有子任务，该项不可修改";
            StartTime.Style.Add("background-color", "#eeeeee");
            EndTime.Style.Add("background-color", "#eeeeee");
            TimeLimit.Style.Add("background-color", "#eeeeee");
        }

        //日程
        if (info.CalendarID != null)
        {
            //chkCalendar.Text = "修改";
            chkCalendar.Checked = true;
           
            CalendarInfo calInfo = (new CalendarBO()).GetInfo(info.CalendarID);
            if (calInfo != null)//如等于null，则该日程给删除了
            {
                drpCalendarTypeID.SelectedValue = calInfo.TypeCode.ToString();
            }

        }
        else
        {
            chkCalendar.Checked = false;
            lblCalendarTypeID.Style.Add("display", "none");
            drpCalendarTypeID.Style.Add("display", "none");
          
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int upperID = int.Parse(UpperID.Value);
        
        PrjTaskInfo info = _task.GetInfo(PKID);

        if (chkCalendar.Checked)
        {
            CalendarBO cal = new CalendarBO();
            CalendarInfo calInfo = cal.GetInfo(info.CalendarID);

            calInfo.Name = Name.Text;
            calInfo.InputTime = DateTime.Now;
            calInfo.JoinerNames = UserNames.Text;
            calInfo.JoinerUserIDs = StrUtil.SplitToInt(UserIDs.Value);
            calInfo.CreatorID = AppSession.UserID;
            calInfo.CreatorName = AppSession.UserName;
            calInfo.TypeCode = int.Parse(drpCalendarTypeID.SelectedValue);
            calInfo.StartTime = DateTime.Parse(StartTime.Text);
            calInfo.EndTime = DateTime.Parse(EndTime.Text);
            calInfo.Content = Remark.Text + "--[项目任务]";

            cal.Save(calInfo);

            info.CalendarID = calInfo.ID;
        }
        else
        {
            if (info.CalendarID != null)
            {
                CalendarBO cal = new CalendarBO();
                cal.Delete(info.CalendarID.Value);
            }
        }

        PrjTaskInfo old = info.Clone();
        EntityUtil.SetEntity(pnlMain, info);
        info.CreatorID = AppSession.UserID;
        info.CreatorName = AppSession.UserName;
        info.PrjID = int.Parse(PrjID);
        if (upperID != Constants.TREE_ROOT_UPPER_ID)
        {
            PrjTaskInfo parentInfo = _task.GetInfo(upperID);
            info.Depth = parentInfo.Depth + 1;
            info.Path = parentInfo.Path + parentInfo.ID + ",";     
        }
        _task.Save(info);
        if (PKID != null)
        {
            TaskChangeBO change = new TaskChangeBO();
            change.Save(info, old, txtReason.Text.Trim());
        }
        JSUtil.RefreshView();
      
    }
}
